/* Name: Probe
 * Author: John Gideon
 * Date: 5-15-12
 * Description: The probe class is used to create probes that are used by the user to explore and
 *  reveal new parts of the map.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Graphics2D;
import java.util.Iterator;
import java.util.LinkedList;

public class Probe extends Item {
	private static LinkedList<Probe> idList = new LinkedList<Probe>();
	private Sprite sprite;
	private float floatX, floatY;
	private float dx, dy;
	final private float SPEED = 3.0f;
	private int lifetime;
	private float rot;
	private float rotSpeed = 0;
	public static int minDistance = 300;
	public static int maxDistance = 1800;
	
	// Used to create a new probe from the given planet at the given coordinates
	public Probe(Planet pid, int fx, int fy) {
		Game.create((Item)this, 100); // Needed for update, draw, and destroy
		colType = COL_CIRCLE; // Needed for collision checking
		idList.add(this); // Needed for collision checking
		floatX = x = pid.x;
		floatY = y = pid.y;
		float dir = (float) Math.atan2(fy-y,fx-x);
		dx = (float) (SPEED*Math.cos(dir));
		dy = (float) (SPEED*Math.sin(dir));
		lifetime = (int) Math.ceil(Math.sqrt(Math.pow(x-fx,2)+Math.pow(y-fy,2)) / SPEED);
		r = 300;
		sprite = Sprite.load("/sprites/satellite.png");
		rot = Game.randomFloat((float) (2*Math.PI));
		if (Game.randomInt(2) == 0) {
			rotSpeed = (float) (Game.randomFloat((float) (2.0f*Math.PI/2500.0f), (float) (2.0f*Math.PI/1250.0f)));
		} else {
			rotSpeed = (float) (Game.randomFloat((float) (-2.0f*Math.PI/1250.0f), (float) (-2.0f*Math.PI/2500.0f)));
		}
		Sound.effect(Sound.EFFECT_PROBE);
	}
	
	// Handles updating info for the Probe
	public void update() {
		x = (int) (floatX += dx);
		y = (int) (floatY += dy);
		Fog.uncover(x, y, r);
		lifetime--;
		if (lifetime <= 0) {
			Game.delete(this);
		}
		
		// Draw
		rot += rotSpeed;
	}
	
	public void collision(Item other) {}
	
	// Deletes the probe
	public void destroy() {
		idList.remove(this); // Needed for collision checking
		
	}
	
	// Called whenever a new frame is drawn - draws the sprite in the new location
	public void draw() {
		Graphics2D gh = Game.getGraphicsHandle();
		sprite.draw(gh, x, y, rot, 1.0f);
	}
	
	// Function needed in each item class for collision checking
	public static int checkCollision(Item other, Boolean notify) {
		int num = 0;
		// Checks for collisions with all other objects, and deletes them if notify=true
	    for(Iterator it = idList.iterator(); it.hasNext();) {
	    	Item idOn = (Item)it.next();
			if (idOn != other) {
				if (Item.intersection(other, idOn)) {
					if (notify) {
						idOn.collision(other);
					}
					num++;
				}
			}
		}
		return num;
	}
}
